import {WeilaPBGroupWrapper} from "@/proto/weilapb_group_wrapper";
import {WL} from "@/proto/weilapb";
import Long from "long";

import {convertFromUserRaw} from "@/proto/weilapb_utils";
import {WL_CoreInterface} from "@/core/weila_datas";
import {Weila_MemDB} from "@/mem_store/weila_db";

export default class WLGroupModule {
    constructor(private coreInterface: WL_CoreInterface) {
        this.coreInterface.registerPbMsgHandler(WL.Service.ServiceID.SERVICE_GROUP, this.onPbMsgHandler.bind(this));
    }

    private onPbMsgHandler(data: any): void {
        const serverMessage = data as WL.Service.IServiceMessage;
        const serviceHead = serverMessage.serviceHead;
        if (serviceHead!.commandType == WL.Service.CommandType.COMMAND_RESPONSE) {
            switch (serviceHead!.commandId) {
                case WL.Group.GroupCommandId.GROUP_COMMAND_GET_GROUPINFO: {
                    this.coreInterface.rspPbMsg(serviceHead!.seq!, serviceHead!.resultCode!, serverMessage!.groupMessage!.rspGetGroupInfo);
                }
                break;

                case WL.Group.GroupCommandId.GROUP_COMMAND_GET_MEMBER_USERINFO: {
                    this.coreInterface.rspPbMsg(serviceHead!.seq!, serviceHead!.resultCode!, serverMessage!.groupMessage!.rspGetMemberUserInfo);
                }
                break;
            }
        }else if (serviceHead!.commandType == WL.Service.CommandType.COMMAND_NOTIFY) {
            switch (serviceHead!.commandId) {
                case WL.Group.GroupCommandId.GROUP_COMMAND_CHANGE_GROUPATTRIBUTE: {
                    const ntfChangeGroupAttr = serverMessage!.groupMessage!.ntfChangeGroupAttribute!.groupAttribute;
                    if (ntfChangeGroupAttr) {
                        const groupId = Long.fromValue(ntfChangeGroupAttr!.groupId!).toString(10);
                        //TODO: 更新群信息
                    }
                }
                break;

            }
        }
    }

    public async getGroupUserInfosFromServer(groupId: string, userIds: number[]): Promise<boolean> {
        const buildResult = WeilaPBGroupWrapper.buildGetGroupMemberInfoReq(Long.fromValue(groupId), userIds);
        if (buildResult.resultCode == 0) {
            try {
                const rspMemberInfo = await this.coreInterface.sendPbMsg(buildResult) as WL.Group.IRspGetMemberUserInfo;
                if (rspMemberInfo.userInfoList) {
                    const userInfos = rspMemberInfo.userInfoList.map((value: WL.Common.IUserInfo) => {
                        return convertFromUserRaw(value);
                    });
                    Weila_MemDB.getInstance().putUsers(userInfos);
                }
                return true;
            }catch (e) {
                return Promise.reject('获取用户失败:' + e);
            }
        }

        return Promise.reject('创建消息失败:' + buildResult.resultCode);
    }

    public async getGroupInfo(groupId: string): Promise<boolean> {
        const buildResult = WeilaPBGroupWrapper.buildGetGroupInfoReq(Long.fromValue(groupId), 0);
        if (buildResult.resultCode == 0) {
            try {
                const rsp = await this.coreInterface.sendPbMsg(buildResult) as WL.Group.IRspGetGroupInfo;
                if (rsp.groupAttribute) {

                }
                return true;
            }catch (e) {
                return Promise.reject('获取群信息失败:' + e);
            }
        }

        return Promise.reject('创建消息失败:' + buildResult.resultCode);
    }
}
